package com.ssb.admin.modules.sys.permission.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ssb.admin.common.utils.TreeBuilder;
import com.ssb.admin.common.utils.TreeNode;
import com.ssb.admin.common.utils.TreeNodeMeta;
import com.ssb.admin.constant.Constants;
import com.ssb.admin.modules.sys.permission.dao.SysPermissionDao;
import com.ssb.admin.modules.sys.permission.entity.SysPermission;
import com.ssb.admin.modules.sys.permission.service.ISysPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.reflect.generics.tree.Tree;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author duaichao
 * @since 2018-08-23
 */
@Service
public class SysPermissionServiceImpl extends ServiceImpl<SysPermissionDao, SysPermission> implements ISysPermissionService {
    private final SysPermissionDao sysPermissionDao;
    @Autowired
    public SysPermissionServiceImpl(SysPermissionDao sysPermissionDao) {
        this.sysPermissionDao = sysPermissionDao;
    }

    @Override
    public List<TreeNode> queryMenusByRoleId(Long roleId) {
        List<SysPermission> permsList;
        //ADMIN角色拥有最高权限
        if (roleId.equals(Constants.USER_SADMIN)) {
            permsList = this.selectList(
                    new EntityWrapper<SysPermission>()
                            .eq("resource_type", "menu")
                            .eq("status",0)
            );
        } else {
            permsList = sysPermissionDao.queryAllMenusByRoleId(roleId);
        }

        return getTreeNodes(permsList, roleId);
    }

    @Override
    public List<TreeNode> queryAllMenus() {
        List<SysPermission> sysPermissions = this.selectList(
                new EntityWrapper<SysPermission>()
                        .eq("status",0));
        return getTreeNodes(sysPermissions, -1);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public void save(SysPermission sysPermission) {
        if (sysPermission.getId()==null) {
            sysPermission.setCreateTime(new Date());
            sysPermission.setUpdateTime(new Date());
            this.baseMapper.insert(sysPermission);
        }else{
            sysPermission.setUpdateTime(new Date());
            this.baseMapper.updateById(sysPermission);
        }
    }

    private List<TreeNode> getTreeNodes(List<SysPermission> sysPermissions, long roleId){
        List<TreeNode> treeNodes = new ArrayList<>(20);
        for (SysPermission sysPermission : sysPermissions) {
            TreeNode treeNode = new TreeNode();
            treeNode.setNodeId(sysPermission.getId());
            treeNode.setName(sysPermission.getName());
            treeNode.setParentId(sysPermission.getParentId());
            treeNode.setPath(sysPermission.getUrl());
            treeNode.setIsDefault(sysPermission.getOpened()==1);
            treeNode.setComponent(sysPermission.getComponent());
            if (roleId != -1) {
                TreeNodeMeta treeNodeMeta = new TreeNodeMeta();
                treeNodeMeta.setIcon(sysPermission.getIcon());
                treeNodeMeta.setTitle(sysPermission.getName());
                treeNodeMeta.setRole(roleId);
                treeNode.setMeta(treeNodeMeta);
            }
            treeNodes.add(treeNode);
        }
        return TreeBuilder.buildListToTree(treeNodes);
    }

}
